# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import re

# import urllib3
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.edge.service import Service
import json
from threading import Thread
from selenium.webdriver.common.by import By
import time


def init_driver():
    """
    初始化浏览器模拟器
    """
    s = Service(r"X:\EdgeDriver\msedgedriver.exe")
    opt = webdriver.EdgeOptions()
    opt.headless = True

    return webdriver.Edge(service=s, options=opt)


def parse(html, driver):
    """
    解析html，生成json文件
    """
    soup = BeautifulSoup(html, "html.parser")
    print(soup.title)

    link_items = soup.find_all('a', href=re.compile(r"https://j.map.baidu.com/*"))

    ret = []
    # item = link_items[40]

    for item in link_items:
        # 分享链接
        shared_url = item.attrs['href']

        # 获取坐标并转换
        lat, lng = get_bd_mcloc_from_share_url(shared_url, driver)
        x, y = translate_bd_loc_to_tencent(lat, lng)

        # 获取名字
        name_str = item.stripped_strings
        name = ""
        for s in name_str:
            name += s

        # 获取段落所有字符
        p = item.find_parent('p')
        str = ""
        for s in p.stripped_strings:
            str += s

        # 整段除去名字，得到评论
        comment = str.replace(name, "")

        d = {'shared_url': shared_url, 'lat': y, 'lng': x, 'name': name, 'comment': comment}
        ret.append(d)
        print(d)

    with open('./data.json', 'w', encoding='utf-8') as f:
        json.dump(ret, f, ensure_ascii=False, indent=4)


def translate_bd_loc_to_tencent(lat, lng):
    """
    百度api，将百度墨卡托坐标转换为腾讯gcj-02坐标
    """
    params = {'coords': lat + ',' + lng, 'ak': 'your ak', 'from': 6, 'to': 5}
    # # params = json.dumps(params)
    # print(params)
    res = requests.get(f"https://api.map.baidu.com/geoconv/v1/?coords={params['coords']}&from=6&to=3&ak={params['ak']}").json()
    # print(res)
    tc_loc = res['result'][0]
    offset = (0.00087393, -4.39568 * 10e-6)
    tc_loc['x'] += offset[0]
    tc_loc['y'] += offset[1]
    # print(tc_loc['y'], tc_loc['x'])

    # 腾讯的转换
    # res_tc = requests.get(f"https://apis.map.qq.com/ws/coord/v1/translate?locations={bd_loc['y']},{bd_loc['x']}&type=3&key=yourkey")
    # print(res_tc.json())

    return tc_loc['x'], tc_loc['y']


def get_bd_mcloc_from_share_url(url, driver):
    """
    根据百度分享链接，获取地点的墨卡托坐标（bdmc）
    """
    driver.get(url)
    time.sleep(0.5)
    url = driver.current_url
    print(url)
    latlng_match = re.search(r".*@(.*?),(.*?),[1-9]+z", url)  # 正则匹配，获取url中的坐标
    lat = latlng_match.group(1)
    lng = latlng_match.group(2)

    return lat, lng

# def get_redirect_url(url):
#     # 无法直接通过request+重定向获取最终链接，因为有js动态加载
#     # 请求头，这里我设置了浏览器代理
#     headers = {
#         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
#     # 请求网页
#     # response = requests.get(url, headers=headers, allow_redirects=False)
#     response = requests.get(url, headers=headers, timeout=(3, 10))
#     print(response.status_code)  # 打印响应的状态码
#     print(response.text)
#     print(response.headers)  # 打印重定向后的网址
#     print(response.url)
#     # 返回重定向后的网址
#     return response.url


if __name__ == '__main__':
    # 初始化driver
    drv = init_driver()
    print('fini')

    # 获取html文档
    drv.get("https://www.navo.top/wx_msgs/spview/4703525/3897331")
    time.sleep(5)
    html = drv.page_source

    with open('./save.html', 'w', encoding='utf-8') as f:
        f.write(BeautifulSoup(html, "html.parser").prettify())

    # 加载并解析
    # with open('./save.html', 'r', encoding='utf-8') as f:
    #     html = f.read()
    parse(html, drv)

